#include "ring_queue.hpp"
#include "Task.hpp"

using namespace ns_ring_queue;
using namespace ns_task;

void* consumer(void* args)
{
    RingQueue<Task>* rq = (RingQueue<Task>*)args;
    while(1)
    {
        sleep(1);
        Task t;
 
        rq->Pop(&t);
        t.run();

        
        // int data = 0;
        // rq->Pop(&data);
        // printf( "消费者消耗数据：%d ,我是：%lu \n" ,data, pthread_self());
    }
}

void* producter(void* args)
{
    RingQueue<Task>* rq = (RingQueue<Task>*)args;
    string ops = "+-*/%";
    while(1)
    {
        sleep(1);
        int x = rand() % 20 + 1;
        int y = rand() % 10 + 1;
        char op = ops[rand() % 5];

        Task t(x, y, op);
        printf("producter【%lu】发送任务: %d %c %d = ?\n\n", pthread_self(), x, op, y);
        rq->Push(t);

        
    //     //1.制造数据
    //     int data = rand() % 50 + 1;
    //    printf("生产者生产数据： %d\n", data);
    //     rq->Push(data);
        
    }
}


int main()
{
    RingQueue<Task>* rq = new RingQueue<Task>();    

    pthread_t c1;
    pthread_t c2;
    pthread_t c3;
    pthread_t p1;
    pthread_t p2;

    pthread_create(&c1, nullptr, consumer, (void*)rq);
    pthread_create(&c2, nullptr, consumer, (void*)rq);
    pthread_create(&c3, nullptr, consumer, (void*)rq);
    pthread_create(&p1, nullptr, producter, (void*)rq);
    pthread_create(&p2, nullptr, producter, (void*)rq);

    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);
    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);
    return 0;
}